package com.rongeadi.app.jinrirong.fragment.home.income;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.rongeadi.app.R;
import com.rongeadi.app.common.adapter.UniFragmentPagerAdapter;
import com.rongeadi.app.common.base.BaseLazyLoadFragment;
import com.rongeadi.app.common.widget.CustomDialog;

/**
 * A simple {@link Fragment} subclass.
 */
public class FatherFragment extends BaseLazyLoadFragment {

    private static final String ARG_INCOME_TYPE = "income_type";
    int type;

    @BindView(R.id.tb_bar)
    TabLayout mTabLayout;

    @BindView(R.id.vp_fragment)
    ViewPager mViewPager;

    public FatherFragment() {
    }

    public static FatherFragment newInstance(int type) {
        FatherFragment fragment = new FatherFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_INCOME_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_father, container, false);
        ButterKnife.bind(this, layout);
        return layout;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        type = getArguments() != null ? getArguments().getInt(ARG_INCOME_TYPE) : 0;
    }

    private   List<String> strings = new ArrayList<>();

    @Override
    protected void lazyLoadData() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(ChildFragment.newInstance(type, 0));
        fragmentList.add(ChildFragment.newInstance(type, 1));

        strings.add("待达标");
        strings.add("已达标");

        mViewPager.setAdapter(new UniFragmentPagerAdapter(getChildFragmentManager(), fragmentList, strings));
        mViewPager.setOffscreenPageLimit(1);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getPosition() == 0){
                    //View view = (View)tab.getCustomView();
                    //TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
                    txt_title.setTextColor(Color.parseColor("#5461eb"));
                }
                else if(tab.getPosition() == 1)
                {
                    txt_title.setTextColor(Color.parseColor("#888888"));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        setupTabIcons();
    }

    private void setupTabIcons() {
        mTabLayout.getTabAt(0).setCustomView(getTabView(0));
    }

    private TextView txt_title;

    public View getTabView(int position) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.tab_item_layout, null);
        txt_title = (TextView) view.findViewById(R.id.txt_title);
        txt_title.setText(strings.get(position));
        txt_title.setTextColor(Color.parseColor("#5461eb"));
        ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
        img_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showNoticeDialog();
            }
        });
        //img_title.setImageResource(tabIcons[position]);
        return view;
    }

    public void showNoticeDialog()
    {
        CustomDialog customDialog = new CustomDialog(getActivity(),R.style.CustomDialog);
        customDialog.setContentView(R.layout.notice_layout);
        customDialog.show();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
}
